package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.NewsPage;
import cn.wolfcode.trip.base.mapper.CollectMapper;
import cn.wolfcode.trip.base.mapper.NewsPageMapper;
import cn.wolfcode.trip.base.query.CollecQueryObject;
import cn.wolfcode.trip.base.query.NewsPageQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsPageService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：大攻略业务层
 *
 * @package: cn.wolfcode.trip.base.service.impl
 * @author: Patch
 * @date: 2019/5/23 22:59
 * 总结:
 */
@Service
public class NewsPageServiceImplImpl implements INewsPageService {
    @Autowired
    private NewsPageMapper newsPageMapper;
    @Autowired
    private CollectMapper collectMapper;

    @Override
    public PageInfo<NewsPage> query(NewsPageQueryObject qo) {
        qo.setOrderBy("creadTime desc");
        qo.setState(qo.getState());
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<NewsPage> list = newsPageMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(NewsPage newsPage) {
        if (newsPage.getId() != null) {
            newsPage.setCreadTime(new Date());
            newsPageMapper.updateByPrimaryKey(newsPage);
        } else {
            newsPage.setCreadTime(new Date());
            newsPageMapper.insert(newsPage);
        }
    }

    @Override
    public NewsPage get(Long id) {
        return newsPageMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<NewsPage> listAll(Integer state) {
        return newsPageMapper.selectAll(state);
    }

    @Override
    public PageInfo<NewsPage> selectCollectDaily(Collect collect, CollecQueryObject qo) {
        //设置当前用户
        collect.setUser(UserContext.getCurrentUser());
        //查询所有收藏内内容的id
        List<Integer> collectIds = collectMapper.selectCollectId(collect);
        //根据获取的内容id查询队该该用户收藏的内容
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<NewsPage> newsPages = newsPageMapper.selectByCollectId(collectIds);
        return new PageInfo<>(newsPages);
    }
}
